﻿using NHibernate;
using NHibernate.Cfg;

namespace Entidades
{
    public static class ConfiguracionNHibernate
    {
        private static ISessionFactory mySessionFactory;
        private static ISession myCurrentSession;
        private static Configuration myConfiguration;

        static ConfiguracionNHibernate()
        {
            myConfiguration = new Configuration();
            myConfiguration.Configure("C:\\hibernate.cfg.xml");
            mySessionFactory = myConfiguration.BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            if (myCurrentSession == null)
            {
                myCurrentSession = mySessionFactory.OpenSession();
            }

            if ((myCurrentSession != null) && (myCurrentSession.IsOpen == false))
            {
                myCurrentSession = mySessionFactory.OpenSession();
            }

            return myCurrentSession;
        }

        public static bool CloseSession()
        {
            if (myCurrentSession != null)
            {
                if (myCurrentSession.IsOpen)
                {
                    myCurrentSession.Close();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
